package com.wimcorp.magic.data.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.springframework.transaction.annotation.Transactional;

import com.wimcorp.magic.data.ColorData;
import com.wimcorp.magic.data.SubTypeData;
@Transactional
public class SubTypeDataDaoImpl implements SubTypeDataDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public long saveOrUpdate(SubTypeData o) {
		if (o.getId() == null) {
			entityManager.persist(o);
			return o.getId();
		} else {
			return entityManager.merge(o).getId();
		}
	}

	@Override
	@Transactional
	public SubTypeData getSubType(String subType) {
		List<SubTypeData> results = new ArrayList<SubTypeData>();
		try {
			
			results = entityManager
					.createQuery("select s from SubTypeData s where subType=:subType", SubTypeData.class)
					.setParameter("subType", subType).getResultList();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		if (results.size() > 0) {
			return results.get(0);
		} else {
			return null;
		}
	}
}
